﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ClientRegister
{
    public partial class ChangeCourseForStuForm : Form
    {
        public string IdCourse { set; get; }
        public string[] IdClass { set; get; }
        public string[] IdSub { set; get; }
        public string IDStu { set; get; }

        public string IDEmp { set; get; }
        public string NameEmp { set; get; }

        DataSet ds;

        SynchronizeData sync = new SynchronizeData();

        public ChangeCourseForStuForm()
        {
            InitializeComponent();
        }

        private void ChangeCourseForStuForm_Load(object sender, EventArgs e)
        {

            ////Load du lieu tu server
            //sync.GetDataForInsertMethodWithNoneID(sync.ClassOfStu, sync.insertcol);
            //sync.GetDataForUpdateMethodWithNoneID(sync.ClassOfStu, sync.updatecol);

            CourseDataProvider loadCour = new CourseDataProvider();
            DataSet ds = loadCour.loadCourseSame(IdCourse, IdSub);
            comboBox1.DataSource = ds.Tables[0];
            comboBox1.DisplayMember = "NameCourse";
            comboBox1.ValueMember = "ID";
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
             if (comboBox1.SelectedValue.ToString() == "System.Data.DataRowView") return;
            else
            {
                ClassDataProvider cls = new ClassDataProvider();
                ds = cls.loadClass(comboBox1.SelectedValue.ToString());
                GridClassInfo.DataSource = ds.Tables[0];
            }
        }

        private void btnChange_Click(object sender, EventArgs e)
        {

            //Load du lieu tu server
            //sync.GetDataForInsertMethodWithNoneID(sync.ClassOfStu, sync.insertcol);
            //sync.GetDataForUpdateMethodWithNoneID(sync.ClassOfStu, sync.updatecol);

            DataTable dtClass = ds.Tables[0];
            StudentInClassDataProvider change = new StudentInClassDataProvider();
         
            string []idnewClass = new string[dtClass.Rows.Count];
            int result = 0;
            //string xml = "";
            for (int i = 0; i < dtClass.Rows.Count; i++)
            {   


                string Note = "";
                SqlDataReader sdr = change.selectClassForStuInfo(IDStu, IdClass[i]);
                if(sdr.Read())
                    Note += sdr.GetValue(3).ToString();
                Note += "Thay doi lop hoc ngay: " + DateTime.Now.ToString() + " Boi nhan vien: ID: " + IDEmp + ", Ten NV: " + NameEmp + "\n";
                Note += "ID lop hoc cu: "+IdClass[i]+ "\n";

                idnewClass[i] = dtClass.Rows[i].ItemArray[0].ToString();

                result = change.changeClassForStudent(IDStu, IdClass[i].ToString(), idnewClass[i].ToString(), Note);
                ////Convert record after update
                //xml += change.selectClassOfStu(IDStu, idnewClass[i]);
                if (result == 0) break;
            }
            if (result != 0)
            {
                ////Chuyen du lieu len server
                //sync.transfDataForUpdateMethod(xml, sync.ClassOfStu);

                MessageBox.Show("Đổi thành công"); 
            }
            else MessageBox.Show("Đổi không thành công");
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
